<?php
include_once dirname(__FILE__).'/../RDBConfig.php';
include_once dirname(__FILE__).'/../../interface/ProjectDAOInterface.php';
include_once dirname(__FILE__).'/../../object/Project.php';

class ProjectDAO implements ProjectDAOInterface {
	
	public static $TABLE_NAME = 'PROJECT';
	public static $COL_PROJECT_PK = 'PROJECT_PK';
	public static $COL_PROJECT_NAME = 'PROJECT_NAME';
	public static $COL_CREATED_DATE = 'CREATED_DATE';
	
	public function __construct() {
	
	}
	
	/**
	 * Them mot dong vao 'PROJECT'.
	 * Tra ve so dong da them
	 * @see ProjectDAOInterface::insert()
	 * @param Project $project
	 * @return int
	 */
	public function insert($project) {
		$table = self::$TABLE_NAME;
		$colProjectPK = self::$COL_PROJECT_PK;
		$colProjectName = self::$COL_PROJECT_NAME;
		$colCreatedDate = self::$COL_CREATED_DATE;
		
		// tao ket noi
		$rdbConfig = new RDBConfig(RDBConfig::$DEFAULT_HOST, RDBConfig::$DEFAULT_DBNAME,
				RDBConfig::$DEFAULT_USERNAME, RDBConfig::$DEFAULT_PASSWORD);
		$conn = RDBDAOFactory::createConnection($rdbConfig);
		
		// tao cau truy van (luu y chu END; cuoi khong co dau cach phia truoc!)
		$statement = <<<END
        INSERT INTO $table
        	($colProjectPK, $colProjectName, $colCreatedDate)
                VALUES (:pro_pk, :pro_name, :created_date)
END;
		$sql = $conn->prepare($statement); // chuan bi cau truy van
		$data = array( // chuan bi du lieu
				':pro_pk' => $project->getProjectPK(),
				':pro_name' => $project->getProjectName(),
				':created_date'=> $project->getCreatedDate()
		);
		
		try {
			$conn->beginTransaction(); // bo che do tu dong commit
		
			$sql->execute($data); // do du lieu vao cau truy van
		
			$conn->commit(); // commit de cap nhat thay doi vao CSDL
		
		} catch (PDOException $excs) { // neu co loi thi cuon lai
			echo $excs->getMessage().'<br>';
			$conn->rollBack();
		}
		
		$conn = null; // free memory
		return $sql->rowCount(); // tra ve so dong da them
	}
	
	/**
	 * Xoa mot dong co PK tuong ung.
	 * Tra ve so dong da xoa.
	 * @see ProjectDAOInterface::delete()
	 * @param Project $project
	 * @return int
	 */
	public function delete($project) {
		$table = self::$TABLE_NAME;
		$colProjectPK = self::$COL_PROJECT_PK;
		$colProjectName = self::$COL_PROJECT_NAME;
		$colCreatedDate = self::$COL_CREATED_DATE;
		
		// tao ket noi
		$rdbConfig = new RDBConfig(RDBConfig::$DEFAULT_HOST, RDBConfig::$DEFAULT_DBNAME,
				RDBConfig::$DEFAULT_USERNAME, RDBConfig::$DEFAULT_PASSWORD);
		$conn = RDBDAOFactory::createConnection($rdbConfig);
		
		// tao cau truy van (luu y chu END; cuoi khong co dau cach phia truoc!)
		$statement = <<<END
        DELETE FROM $table
        	WHERE $colProjectPK = :pro_pk
END;
		$sql = $conn->prepare($statement); // chuan bi cau truy van
		$data = array( // chuan bi du lieu
				':pro_pk' => $project->getProjectPK()
		);
		
		try {
			$conn->beginTransaction(); // bo che do tu dong commit
		
			$sql->execute($data); // do du lieu vao cau truy van
		
			$conn->commit(); // commit de cap nhat thay doi vao CSDL
		
		} catch (PDOException $excs) { // neu co loi thi cuon lai
			echo $excs->getMessage().'<br>';
			$conn->rollBack();
		}
		
		$conn = null; // free memory
		return $sql->rowCount(); // tra ve so dong da them
	}
	
	/**
	 * Cap nhat mot dong co PK = $old->PK (khong cap nhat cot PK).
	 * Tra ve so dong da cap nhat.
	 * @see ProjectDAOInterface::update()
	 * @param Project $old
	 * @param Project $new
	 * @return int
	 */
	public function update($old, $new) {
		$table = self::$TABLE_NAME;
		$colProjectPK = self::$COL_PROJECT_PK;
		$colProjectName = self::$COL_PROJECT_NAME;
		$colCreatedDate = self::$COL_CREATED_DATE;
		
		// tao ket noi
		$rdbConfig = new RDBConfig(RDBConfig::$DEFAULT_HOST, RDBConfig::$DEFAULT_DBNAME,
				RDBConfig::$DEFAULT_USERNAME, RDBConfig::$DEFAULT_PASSWORD);
		$conn = RDBDAOFactory::createConnection($rdbConfig);
		
		// tao cau truy van (luu y chu END; cuoi khong co dau cach phia truoc!)
		$statement = <<<END
        UPDATE $table
        	SET
        		$colProjectName = :pro_name,
        		$colCreatedDate = :created_date
        	WHERE $colProjectPK = :pro_pk
END;
		$sql = $conn->prepare($statement); // chuan bi cau truy van
		$data = array( // chuan bi du lieu
				':pro_pk' => $old->getProjectPK(),
				':pro_name' => $new->getProjectName(),
				':created_date' => $new->getCreatedDate()
		);
		
		try {
			$conn->beginTransaction(); // bo che do tu dong commit
		
			$sql->execute($data); // do du lieu vao cau truy van
		
			$conn->commit(); // commit de cap nhat thay doi vao CSDL
		
		} catch (PDOException $excs) { // neu co loi thi cuon lai
			echo $excs->getMessage().'<br>';
			$conn->rollBack();
		}
		
		$conn = null; // free memory
		return $sql->rowCount(); // tra ve so dong da cap nhat
	}
	
	/**
	 * Tra ve mot Project co PK tuong ung, neu khong tra ve Project(NULL)
	 * @see ProjectDAOInterface::getProjectByPK()
	 * @param int $projectPK
	 * @return Project
	 */
	public function getProjectByPK($projectPK) {
		$table = self::$TABLE_NAME;
		$colProjectPK = self::$COL_PROJECT_PK;
		$colProjectName = self::$COL_PROJECT_NAME;
		$colCreatedDate = self::$COL_CREATED_DATE;
		
		$statement = <<<END
        SELECT *
        FROM $table
        WHERE $colProjectPK = :pro_pk
        LIMIT 1
END;
		// tao ket noi
		$rdbConfig = new RDBConfig(RDBConfig::$DEFAULT_HOST, RDBConfig::$DEFAULT_DBNAME,
				RDBConfig::$DEFAULT_USERNAME, RDBConfig::$DEFAULT_PASSWORD);
		$conn = RDBDAOFactory::createConnection($rdbConfig);
		
		$sql = $conn->prepare($statement); // chuan bi cau truy van
		$data = array( // chuan bi du lieu
				':pro_pk' => $projectPK
		);
		
		try {
			$sql->execute($data); // do du lieu vao cau truy van
			$sql->setFetchMode(PDO::FETCH_ASSOC);
			if($sql->rowCount() > 0) {
				$row = $sql->fetch();
				$pro = new Project($row["$colProjectPK"],
						$row["$colProjectName"], $row["$colCreatedDate"]);
				return $pro;
			}
				
		} catch (PDOException $excs) { // neu co loi thi cuon lai
			echo $excs->getMessage().'<br>';
		}
		$conn = null; // free memory
		
		return new Project(0, null, null);
	}
	
	/**
	 * Tra ve gia tri Project PK lon nhat
	 * @see ProjectDAOInterface::getMaxPK()
	 * @return int
	 */
	public function getMaxPK() {
		$table = self::$TABLE_NAME;
		$colProjectPK = self::$COL_PROJECT_PK;
		$colProjectName = self::$COL_PROJECT_NAME;
		$colCreatedDate = self::$COL_CREATED_DATE;
	
		$statement = <<<END
        SELECT MAX($colProjectPK)
        FROM $table
END;
		// tao ket noi
		$rdbConfig = new RDBConfig(RDBConfig::$DEFAULT_HOST, RDBConfig::$DEFAULT_DBNAME,
				RDBConfig::$DEFAULT_USERNAME, RDBConfig::$DEFAULT_PASSWORD);
		$conn = RDBDAOFactory::createConnection($rdbConfig);
	
		$sql = $conn->prepare($statement); // chuan bi cau truy van
	
		try {
			$sql->execute(); // do du lieu vao cau truy van
			$sql->setFetchMode(PDO::FETCH_ASSOC);
			if($sql->rowCount() > 0) {
				$row = $sql->fetch();
				$max = $row["MAX($colProjectPK)"];
				return $max;
			}
	
		} catch (PDOException $excs) { // neu co loi thi cuon lai
			echo $excs->getMessage().'<br>';
		}
		$conn = null; // free memory
	
		return 0;
	}
}
?>
